using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Controls_AddProduct : System.Web.UI.UserControl
{
    private bool ClearFormB;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ClearFormB = false;
        }
        if (ClearFormB == true)
        {
           // this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "clearform1", "clear_form()", true);
            ClearFormB = false;
        }
        if (ViewState["StyleList"] == null)
        {
            DataSet Sds = new DataSet();
            DataTable t = new DataTable();
            DataColumn s = new DataColumn("StyleId");
            DataColumn n = new DataColumn("StyleName");
            DataColumn d = new DataColumn("DropshipId");
            t.Columns.Add(s);
            t.Columns.Add(n);
            t.Columns.Add(d);
            Sds.Tables.Add(t);
            
            ViewState["StyleList"] = Sds;
        }

        ddlType.DataSource = Enum.GetValues(typeof(DD.Products.ProductType));
        //ddlType.DataValueField = Enum..GetValues(typeof(DD.Products.ProductType));

        ddlType.DataBind();

        lbCategories.DataSource = DD.Data.SQL.CategoryManager.GetCategories();
        lbCategories.DataTextField = "CAT_NAME";
        lbCategories.DataValueField = "CAT_ID";
        lbCategories.DataBind();

        
        ddlStyles.DataSource = DD.Data.SQL.StyleManager.GetStyles();
        ddlStyles.DataTextField = "NAME";
        ddlStyles.DataValueField = "SID";
        ddlStyles.DataBind();

        //Button2.Attributes.Add("onclick", "return confirm_delete('Are you sure you want to add this');");


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DD.Products.Product p = new DD.Products.Product();
        p.Name = txtProdName.Text;
        p.Cost = Convert.ToDouble(txtCost.Text.Replace("$", ""));
        p.Price = Convert.ToDouble(txtPrice.Text.Replace("$", ""));
        p.SalePrice = Convert.ToDouble(txtSale.Text.Replace("$", ""));
        p.OnSale = (cbOnSale.Checked ? true : false);
        p.Active = (cbProductActive.Checked ? true : false);
        p.Description = txtDesc.Text;
        p.Type = (DD.Products.ProductType)Enum.Parse(typeof(DD.Products.ProductType), ddlType.SelectedValue);
        

        int NewId = DD.Products.ProductManager.AddProduct(p);
        AddStyles(NewId);
        AddCategories(NewId);
        ClearFormB = true;

        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "show_alert('The product has been added')", true);
      //  this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "clearform", "clear_form()", true);
        //Page.RegisterStartupScript("Message", "return show_alert('The product has been successfully added')");

        ClearForm();
    }

    private void AddStyles(int id)
    {
        DataSet ds = (DataSet)ViewState["StyleList"];

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            DD.Products.ProductManager.AddProductStyle(id, Convert.ToInt32(dr[0]), dr[2].ToString());

        }

    }
    private void AddCategories(int id)
    {
        foreach (ListItem li in lbCategories.Items)
        {
            if (li.Selected == true)
            {
                DD.Products.ProductManager.AddProductCategory(id, Convert.ToInt32(li.Value));
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int SID = Convert.ToInt32(ddlStyles.SelectedValue);
        string DID = txtDropShip.Text;

        DataSet ds = (DataSet)ViewState["StyleList"];

        DataRow dr = ds.Tables[0].NewRow();
        dr[0] = SID;
        dr[1] = ddlStyles.SelectedItem.Text;
        dr[2] = DID;

        ds.Tables[0].Rows.Add(dr);

        ViewState["StyleList"] = ds;

        BuildSDList();
    }

    private void BuildSDList()
    {
        lbDropStyle.Items.Clear();
        DataSet ds = (DataSet)ViewState["StyleList"];
        int count = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            ListItem li = new ListItem();
            int SID = Convert.ToInt32(dr[0]);
            string DID = dr[2].ToString();
            string Sname = dr[1].ToString();
            li.Text = Sname + " -- " + DID;
            li.Value = count.ToString();
            lbDropStyle.Items.Add(li);
            lbDropStyle.DataBind();
            count++;
        }
            
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        if (lbDropStyle.SelectedIndex > -1)
        {
            DataSet ds = (DataSet)ViewState["StyleList"];

            int SID = Convert.ToInt32(lbDropStyle.SelectedValue);

            ds.Tables[0].Rows.RemoveAt(Convert.ToInt32(lbDropStyle.SelectedValue));

            BuildSDList();
        }
        
    }

    private void ClearForm()
    {
        lbCategories.SelectedIndex = -1;
        Response.Redirect(Request.RawUrl);
        txtCost.Text = "";
        txtDesc.Text = "";
        txtDropShip.Text = "";
        txtPrice.Text = "";
        txtProdName.Text = "";
        txtSale.Text = "";

        lbDropStyle.Items.Clear();
        ViewState["StyleList"] = null;
       
    }
}
 
